package org.irri.smta.service.impl;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.irri.smta.service.IMessageProviderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named("messageProviderService")
public class MessageProviderService implements IMessageProviderService<String> {

	private static final Logger LOGGER = LoggerFactory.getLogger(MessageProviderService.class);
	
	private ResourceBundle bundle;

	public ResourceBundle getBundle() {
		LOGGER.debug("getBundle(): " + bundle);
		if (bundle == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			bundle = context.getApplication().getResourceBundle(context, "messages");
		}
		return bundle;
	}

	@Override
	public String getMessage(String key) {
		LOGGER.debug("getMessage(String key): " + key);
		String message = null;
		try {
			message = getBundle().getString(key);
		} catch (MissingResourceException e) {
			message = "???" + key + "??? not found";
		}
		return message;
	}

}
